package db_svr

import (
	"fmt"

	"gopkg.in/rana/ora.v3"
)

type Incomeing_msg struct {
	Remote_IP_Addr string
	Remote_IP_Port string
	Device_id      string
	Data_type      string
	Data_str       string
	Data_dtl       []string
}

type Ua_retMsg struct {
	Flag    int8
	Message string
	User_id int
	Data    string
}

type dev_retMsg struct {
	Flag    int8
	Message string
	Data    string
}

func Refcursor_to_string(p_flag int8, p_ret_msg string, procRset *ora.Rset) dev_retMsg {
	// logger = log.New(os.Stdout, ">>>>>DB.Ref Cursor To String :", log.LstdFlags|log.Lshortfile)
	//    logger.Printf("\n=================ora.Rset ==============================\n [%T] : %v   ", procRset )

	var r, v_data string

	if p_flag != 0 {
		return dev_retMsg{Flag: p_flag,
			Message: p_ret_msg,
			Data:    fmt.Sprintf(`{"flag":%v,"message":"%v","data":[ ]}`, p_flag, p_ret_msg)}
	}
	if procRset.IsOpen() {
		//        logger.Println( "\n ............. procRset Is open ....... ")
		for procRset.Next() {
			r = fmt.Sprintf("%v", procRset.Row[0])
			if v_data == "" {
				v_data += r
			} else {

				v_data += "," + r

			}
		}
		if procRset.Err != nil {
			logger.Panic(procRset.Err)
		}
	} else {

		v_data = ""
	}

	return dev_retMsg{
		Flag:    p_flag,
		Message: p_ret_msg,
		Data:    fmt.Sprintf(`{"flag":%v,"message":"%v","data":[%v]}`, p_flag, p_ret_msg, v_data),
	}

}
